<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<body>
<input type="text" id="questionInput">
<button onclick="sendQuestion()">Send</button>
<button onclick="clearContext()">End</button> <!-- 添加清空上下文按钮 -->
<div id="responseContainer"></div>

<script>
    function sendQuestion() {
        var question = document.getElementById('questionInput').value;
        var url = 'http://localhost:8080/chat?q=' + encodeURIComponent(question);

        fetch(url)
            .then(function(response) {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error('Request failed');
                }
            })
            .then(function(response) {
                // 处理响应
                var chatGPTResponse = response;

                // 将ChatGPT的响应显示在页面上
                document.getElementById('responseContainer').textContent = chatGPTResponse;
            })
            .catch(function(error) {
                // 处理错误
                console.error(error);
            });
    }

    function clearContext() {
        fetch('http://localhost:8080/clear-context', { method: 'POST' })
            .then(function(response) {
                if (response.ok) {
                    // 清空上下文成功
                    console.log('Context cleared');
                } else {
                    throw new Error('Request failed');
                }
            })
            .catch(function(error) {
                // 处理错误
                console.error(error);
            });
    }
</script>
</body>
</html>